//
// 按采购人员
//
<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <SearchBar :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          ref="table1"
          :data="tableData"
          stripe
          border
          :height="tableHeight"

          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          show-summary
          :summary-method="getSummaries"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" width="70" align="center" />
          <Column
            v-for="(column,index) in tableColumns"
            :key="index"
            :prop="column.prop"
            :is-show="column.isShow"
            :label="column.label"
            :sortable="column.sortable"
            :width="column.width"
            :min-width="column.minWidth"
            :align="column.align"
          />
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>

  </div>
</template>

<script>
import { listForSaleCollect } from '@/api/purchaseManage/purchaseStatistics/purchaseCollectMenu'
import { getDictSelects } from '@/api/common/common'
export default {
  name: 'ByMaterial',
  data() {
    return {
      loading: true,
      pageId: '采购按采购人员汇总表', // 页面唯一标识
      pageExportId: '采购按采购人员汇总表导出',
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'orderName',
        isAsc: 'desc',
        byColumn: 'orderName'
      },
      totalWord: ['purchaseNumber', 'allTotalTaxPrice'], // 合计导出字段
      // tableKey: 1,
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData2: [], // 导出表格数据
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'orderName', label: '采购人员' },
        { prop: 'materialCode', label: '物料编码' },
        { prop: 'materialName', label: '物料名称', canOrder: false },
        { prop: 'materialModel', label: '规格型号', canOrder: false },
        { prop: 'materialUnitLabel', label: '物料单位', canOrder: false },
        { prop: 'purchaseNumber', label: '采购数量', align: 'right' },
        { prop: 'taxAveragePriceThousandths', label: '含税均价', align: 'right' },
        { prop: 'allTotalTaxPriceThousandths', label: '采购金额', align: 'right' }
      ],
      exportTableColumns: [
        { prop: 'orderName', label: '采购人员' },
        { prop: 'materialCode', label: '物料编码' },
        { prop: 'materialName', label: '物料名称', canOrder: false },
        { prop: 'materialModel', label: '规格型号', canOrder: false },
        { prop: 'materialUnitLabel', label: '物料单位', canOrder: false },
        { prop: 'purchaseNumber', label: '采购数量', align: 'right' },
        { prop: 'taxAveragePrice', label: '含税均价', align: 'right' },
        { prop: 'allTotalTaxPrice', label: '采购金额', align: 'right' }
      ],
      defaultExport: [],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'orderName', label: '采购人员', type: 'input' },
        { prop: 'materialCode', label: '物料编码', type: 'input' },
        { prop: 'materialName', label: '物料名称', type: 'input' },
        { prop: 'materialModel', label: '规格型号', type: 'input' },
        { prop: 'materialUnitLabel', label: '物料单位', type: 'dict', optionsName: 'statusOptions' },
        { prop: 'orderTime', label: '采购日期', clearable: false, type: 'time', isTime: true, hasDefault: true, defaultKey: 'orderTimeDefault' }
      ],
      operationData: [ // 右上角的操作按钮
        { label: '批量导出2', perm: '' },
        { label: '页面配置', perm: '' }
      ],
      statusOptions: [],
      Updata: [],
      orderTimeDefault: [] // 存放字段的默认值
    }
  },
  created() {
    this.orderTimeDefault = [this.$moment().startOf('month').format('YYYY-MM-DD') + ' 00:00',
      this.$moment(new Date()).format('YYYY-MM-DD') + ' 23:59']
  },
  mounted() {
    this.getDictSelects()
  },
  methods: {
    getList() {
      if (!this.pageData.byColumn) {
        this.pageData.byColumn = 'orderName'
      }
      listForSaleCollect(this.pageData).then(res => {
        this.tableData = res.data.dataTable.data
        this.total = res.data.dataTable.total
        this.Updata = res.data
        this.loading = false
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
          this.$refs.table1.doLayout()
        })
      })
    },
    // 物料单位字典
    getDictSelects() {
      getDictSelects('materialUnitId').then(res => {
        this.statusOptions = res.data
        res.data.unshift({ dictCode: undefined, dictLabel: '全部' },)
      })
    },
    getExportData(data) {
      return new Promise(function(resolve, reject) {
        listForSaleCollect(data).then(res => {
          resolve(res.data.dataTable.data)
        })
      })
    },
    // 合计行
    getSummaries(param) {
      const {
        columns
      } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index == 0) {
          sums[index] = '合计'
          return
        }
        switch (column.property) {
          case 'purchaseNumber':
            sums[index] = this.Updata.totalPurchaseNumber
            break
        }
        switch (column.property) {
          case 'allTotalTaxPrice':
            sums[index] = this.Updata.totalAllTaxPrice
            break
        }
      })
      return sums
    }
  }
}
</script>

<style lang="less" scoped>
.main_header{
  .padding_container{
    justify-content: flex-end;
  }
}
</style>
